#include "zmq.h"
#include "stdio.h"
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char * argv[]) {
    if (argc < 2) {
        printf("Usage: %s [count]\n", argv[0]);
        return -1;
    }
    printf("hello\n");

    void* context = zmq_ctx_new();
    assert(context != NULL);

    int ret = zmq_ctx_set(context, ZMQ_MAX_SOCKETS, 1);
    assert(ret == 0);

    void* publisher = zmq_socket(context, ZMQ_PUB);
    assert(publisher != NULL);

    ret = zmq_bind(publisher, "tcp://0.0.0.0:9999");
    assert(ret == 0);

    sleep(5);
    printf("start publish");

    int count = atoi(argv[1]);
    char szData[16] = { 0 };
    for (int i = 0; i < count; ++i) {
        snprintf(szData, sizeof(szData), "num:%d", i+1);
        //ret = zmq_send(publisher, "Hi, I'm roger", 16, 0);
        ret = zmq_send(publisher, szData, strlen(szData), 0);
        printf("%d\n", ret);
        usleep(1);
    }
    /*
    while (1) {
        ret = zmq_send(publisher, "Hi, I'm roger", 16, 0);
        printf("%d\n", ret);
        sleep(1);
    }*/
    return 0;
}

